package promentheus

import (
	"github.com/pkg/errors"
	"time"
)

type Date struct {
	Start     time.Time     //开始时间
	End       time.Time     //截至时间
	Step      time.Duration //采样点的时间间隔（单位ns）
	Formatter string        //格式化模板
	SampleNum int64         //采样点数量
}

//时间条件的处理，和时间格式化的格式
func NewDate(startStr, endStr string, sampleNum int64, timeStep int64) (*Date, error) {
	start, err := time.ParseInLocation("2006-01-02 15:04:05", startStr, time.Local)
	if err != nil {
		return nil, errors.Wrap(err, "start time convert error")
	}
	end, err := time.ParseInLocation("2006-01-02 15:04:05", endStr, time.Local)
	if err != nil {
		return nil, errors.Wrap(err, "end time convert error")
	}
	if end.UnixNano()-start.UnixNano() <= 0 {
		return nil, errors.Errorf("%s", "end time cannot before start time")
	}
	formatter := "2006-01-02 15:04:05"
	var step time.Duration
	if timeStep != int64(0) {
		step = time.Duration(timeStep) * time.Second
		sampleNum = (end.UnixNano()-start.UnixNano())/int64(step) + 1
	} else {
		step = time.Duration((end.UnixNano() - start.UnixNano()) / (sampleNum - 1))
	}

	return &Date{
		Start:     start,
		End:       end,
		Step:      step,
		Formatter: formatter,
		SampleNum: sampleNum,
	}, nil
}

//将数值扩大或缩小几倍
//func EnlargeNum(value model.SampleValue, multiple float64, str string) model.SampleValue {
//	if str == "*" {
//		value = value * model.SampleValue(multiple)
//	} else if str == "/" {
//		value = value / model.SampleValue(multiple)
//	}
//	valStr := fmt.Sprintf("%.3f", float64(value))
//	val, err := strconv.ParseFloat(valStr, 64)
//	if err == nil {
//		value = model.SampleValue(val)
//	}
//	return value
//}
